home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 331_01 / ged10.c < prev    next >
Text File  |  1990-06-12  |  4KB  |  185 lines

  1. /*
  2. HEADER:         CUG999.10;
  3. TITLE:          GED (nee QED) screen editor -- part 10;
  4. DATE:           10/10/86;
  5.  
  6. DESCRIPTION:    "Disc interface functions";
  7. FILENAME:       GED10.C;
  8. AUTHORS:        G. Nigel Gilbert, James W. Haefner, Mel Tearle, G. Osborn;
  9. */
  10.  
  11. /*
  12.      e/qed/ged  screen editor
  13.  
  14.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  15.            August-December 1981
  16.  
  17.     Modified:  Aug-Dec   1984:   BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  18.                March     1985:   BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  19.                May       1986:   converted to ged - Mel Tearle
  20.  
  21.     FILE:      ged10.c
  22.  
  23.     FUNCTIONS: fopen1, fcreat, creatb, dflush, fclose, eputc, egetc, funlink,
  24.                frename, lseek1
  25.  
  26.     PURPOSE:   file buffer operations extracted and stripped from stdlib1,
  27.                with protection from bdos error crashes added
  28.  
  29. */
  30.  
  31. /* These I/O functions are relatively low level.  They could be replaced
  32.  * by higher level and simpler functions, but there are timing consideratons
  33.  * which should be evaluated first.  Microsoft provides some additional
  34.  * error information which is not utilized here.    g.o.
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include "ged.h"
  39.  
  40. /* Open an existing file for character input (writing not allowed).
  41.  * The text file is opened in the binary mode.
  42.  */
  43.  
  44. int fopen1(filename,iobuf)  /* returns file handle */
  45. struct iobuffer *iobuf;
  46. char   *filename;
  47. {
  48.     int i;
  49.  
  50.     i = open(filename, 0x8000);   /* read only, binary */
  51.     iobuf -> fdes = i;
  52.     if (i < 0) {
  53.         return  FAIL;
  54.     }
  55.     else {
  56.         iobuf -> nleft = 0;
  57.         return  i;
  58.     }
  59. }
  60.  
  61.  
  62. /* create a file to be used for reading and writing characters.  truncate
  63.  * an existing file.
  64.  * read and write permission, binary mode.  Evidently compiler dependent.
  65.  * Note that the system call for binary and text I/O are the same.
  66.  */
  67. int fcreat(name,iobuf)
  68. char   *name;
  69. struct iobuffer *iobuf;
  70. {
  71.     if ( ( iobuf -> fdes = open(name, 0x8302, 0x180)) < 0 )
  72.         return  FAIL;
  73.     iobuf -> nextp = iobuf -> buff;
  74.     iobuf -> nleft = ( NSECTS * SECSIZ );
  75.     return  iobuf -> fdes;
  76. }
  77.  
  78. /* create a file to be used for reading and writing binary blocks.  truncate an
  79.  * existing file.  read and write permission, binary mode.
  80.  * returns file handle.
  81.  */
  82. int creatb(name)
  83. char   *name;
  84. {
  85.     int fd;
  86.     if ( ( fd = open(name, 0x8302, 0x180)) < 0 )
  87.         return  FAIL;
  88.     else
  89.         return  fd;
  90. }
  91.  
  92.  
  93. /* The seek is relative to the beginning of the file.
  94.  * Included here for consolidation purposes to simplify disc i/o
  95.  * compiler conversion.
  96.  */
  97. long int lseek1(fd, loc)
  98. int fd;
  99. long int loc;
  100. {
  101.     long int lseek();
  102.  
  103.     return lseek( fd, loc, 0 );
  104. }
  105.  
  106.  
  107. /* does not allow more writing
  108.  */
  109. int dflush(iobuf)
  110. struct  iobuffer *iobuf;
  111. {
  112.     int  i;
  113.  
  114.     if ( iobuf -> nleft == ( NSECTS * SECSIZ ) )
  115.         return  YES;
  116.     i = NSECTS*SECSIZ - iobuf->nleft;
  117.     if ( write( iobuf -> fdes, iobuf -> buff, i ) != i )
  118.         return  FAIL;
  119.     return YES;
  120. }
  121.  
  122.  
  123. int fclose(iobuf)
  124. struct iobuffer *iobuf;
  125. {
  126.     return  close( iobuf -> fdes );
  127. }
  128.  
  129.  
  130. /* stripped down version of standard putc
  131.  */
  132. eputc(c,iobuf)
  133. char    c;
  134. struct  iobuffer *iobuf;
  135. {
  136.     if ( iobuf -> nleft-- )
  137.         return  *iobuf -> nextp++ = c;
  138.     if ( ( write(iobuf -> fdes, iobuf -> buff, NSECTS*SECSIZ)) != NSECTS*SECSIZ )
  139.         return  FAIL;
  140.     iobuf -> nleft = ( NSECTS * SECSIZ -1 );
  141.     iobuf -> nextp = iobuf -> buff;
  142.  
  143.     return  *iobuf -> nextp++ = c;
  144. }
  145.  
  146.  
  147. /* the standard getc, trimmed for speed
  148.  */
  149. int egetc(iobuf)
  150. struct iobuffer  *iobuf;
  151. {
  152.     int nbytes;
  153.  
  154.     if ( iobuf -> nleft-- )
  155.         return  *iobuf -> nextp++;
  156.     if ( (nbytes = read(iobuf -> fdes, iobuf -> buff, NSECTS*SECSIZ)) == -1 )  {
  157.         iobuf -> nleft++;
  158.         return  DFAIL;
  159.     }
  160.     if ( nbytes == 0 )  {
  161.         iobuf -> nleft++;
  162.         return  ENDFILE;
  163.     }
  164.     iobuf -> nleft = nbytes - 1;
  165.     iobuf -> nextp = iobuf -> buff;
  166.  
  167.     return  *iobuf->nextp++;
  168. }
  169.  
  170. /* Erase a disc file */
  171. funlink(name)
  172. char *name;
  173. {
  174. /* if (dskcheck(setjmp(dskerr)))  return FAIL; */
  175.     return  unlink( name );
  176. }
  177.  
  178.  
  179. frename(oldname,newname)
  180. char *oldname,*newname;
  181. {
  182. /* if (dskcheck(setjmp(dskerr)))  return FAIL; */
  183.     return  rename( oldname, newname );
  184. }
  185.